import Vue from 'vue'
import vueResource from 'vue-resource'
Vue.use(vueResource);

let fetch = (info) => {
	let promise = new Promise(function(resolve, reject){
		if(info.type == 'post'){
			Vue.http.post(
				info.url, 
				info.params,
				{ credentials: true, emulateJSON: true }
			)
			.then(
				response => {

					resolve(response.body);

				}, 
				response => {

					reject();

				}
			);
			
		}else{
			// console.log(info.params)
			Vue.http.jsonp(
				info.url, 
				{
					params: info.params,
					jsonp: info.jsonp || 'callback',
					jsonpCallback: info.jsonpCallback
				}
			)
			.then(
				response => {

					resolve(response.body);

				}, 
				response => {

					reject();

				}
			);
		}

	});
	return promise;
};

export default fetch
